package com.price.common.viewbinding.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.price.common.extend.doCustomCrashListener
import com.price.common.viewbinding.FragmentBinding
import com.price.common.viewbinding.FragmentBindingDelegate

abstract class BaseBindingFragment<VB : ViewBinding> : Fragment(),
    FragmentBinding<VB> by FragmentBindingDelegate() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) =
        createViewWithBinding(inflater, container)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        runCatching {
            onCreated(savedInstanceState)
        }.onFailure {e->
            doCustomCrashListener?.onCrash(e)
        }
    }

    abstract fun onCreated(savedInstanceState: Bundle?)

    override fun onResume() {
        super.onResume()
        runCatching {
            resume()
        }.onFailure {e->
            doCustomCrashListener?.onCrash(e)
        }
    }

    override fun onPause() {
        super.onPause()
        runCatching {
            pause()
        }.onFailure {e->
            doCustomCrashListener?.onCrash(e)
        }
    }
    open fun resume(){}
    open fun pause(){}

}